package api

import (
	"encoding/json"
	"net/http"
)

type ApiResult struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
	Data any    `json:"data"`
}

func Failed(rw http.ResponseWriter, err string) {
	write(rw, ApiResult{Code: 0, Data: nil, Msg: err})
}

func Success(rw http.ResponseWriter, t any) {
	write(rw, ApiResult{Code: 1, Data: t, Msg: "ok"})
}

func write(rw http.ResponseWriter, apiResult ApiResult) {
	result, _ := json.Marshal(apiResult)
	rw.WriteHeader(200)
	rw.Write(result)
}
